from machine import Pin,PWM# 导入Pin模块
import time# 导入time模块

beep = PWM(Pin(12),freq = 200,duty = 512)#构建beep对象，GPIO输出

#创建C调谱音律频率对照
freq_dict = {-1:262,-2:294,-3:330,-4:349,-5:392,
-6:440,-7:494,1:523,2:587,3:659,4:698,5:784,
6:880,7:988,11:1046,21:1175,31:1318,41:1397,51:1568,61:1760,71:1976}
tone = [-1,-2,-3,-4,-5,-6,-7,1,2,3,4,5,6,7,11,21,31,41,51,61,71]

#乐谱
music_list = [
  (11, 1), (5, 0.75), (5, 0.25), 
  (6, 0.5), (11, 0.5), (5, 1), 
  (11, 1), (5, 0.75), (5, 0.25), 
  (6, 0.5), (11, 0.5), (5, 1), 
  (6, 1), (5, 0.75), (6, 0.5), 
  (3, 0.5), (2, 0.5), (3, 1), 
  (6, 1), (5, 0.75), (6, 0.25), 
  (3, 0.5), (2, 0.5), (1, 1), 
  (6, 1), (5, 0.5), (6, 0.5), 
  (11, 1.5), (6, 0.5), 
  (5, 0.5), (5, 0.25), (5, 0.25), (3, 0.5), (5, 0.5), 
  (6, 1.5), (0, 0.5), 
  (3, 1), (2, 0.5), (3, 0.5), 
  (5, 1.5), (6, 0.5), 
  (3, 0.5), (2, 0.25), (2, 0.25), (1, 0.5), (3, 0.5), 
  (2, 1.5), (0, 0.5), 
  (6, 1.5), (5, 0.5), 
  (1, 1), (1, 0.5), (6, 0.5), 
  (5, 1), (3, 0.5), (5, 0.5), 
  (6, 1.5), (0, 0.5), 
  (3, 1.5), (2, 0.5), 
  (5, 1), (6, 0.5), (11, 0.5), 
  (3, 1), (2, 0.5), (3, 0.5), 
  (1, 1.5), (0,0.5), 
  (11, 0.5), (11, 0.25), (21, 0.25), (11, 0.5), (6, 0.5), 
  (5, 1), (3, 0.5), (5, 0.5), 
  (6, 0.75),(5, 0.25), (3, 0.5), (5, 0.5), 
  (11, 0.5), (6, 0.5), (5, 0.5), (0, 0.5), 
  (11, 0.75), (21, 0.25), (11, 0.5), (6, 0.5), 
  (5, 1), (3, 0.5), (0, 0.5), 
  (5, 0.75), (5, 0.25),(6, 0.5), (11, 0.5), 
  (5, 1.5), (6, 0.5), 
  (11, 0.75), (11, 0.25), (11, 0.5), (31, 0.5), 
  (22, 0.5), (22, 0.5), (22, 0.5), (6, 0.5), 
  (11, 2), (11, 1), (0, 1)
]

for i in music_list:
    tone_i,time_i = i[0],i[1]
    time_i = time_i / 2 #匹配原曲速度
    print(tone_i,end=',')
    # print(time_i,end=',')
    if tone_i in tone: 
        beep.freq(freq_dict[tone_i])
        time.sleep(time_i)
    elif tone_i == 0: #空拍
        beep.duty(0)
        time.sleep(time_i)
        beep.duty(512)
    else:
        pass
beep.deinit() #关闭蜂鸣器